﻿using System;
using System.Web;
using System.Web.UI;
using SimplePMT.Data;
using SimplePMT.Control;
using System.Text;
using System.Collections.Generic;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace SimplePMT
{
    public partial class Notification : Components.BasePage
    {

        public int fileid = 0;
        public int taskId = 0;
        public string notificationView = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Notification), this.Page);

            Control.NotificationControl view = this.LoadControl("./Control/NotificationControl.ascx") as Control.NotificationControl;
            if (Request.QueryString["attachmentid"] != null)
            {
                view.attachmentid = Convert.ToInt32(Request.QueryString["attachmentid"]);
            }
            else
            {
                view.taskid = Convert.ToInt32(Request.QueryString["taskid"]);
            }
            notificationView = view.ToString();
        }

        [AjaxPro.AjaxMethod()]
        public bool AddTaskNote(int taskId, string note, int MemberID)
        {
            try
            {
                TaskNote taskNote = new TaskNote();
                taskNote.IsSVNSource = false;
                taskNote.Member = Member.Find(MemberID);
                taskNote.Notes = note;
                taskNote.DateTime = DateTime.Now;
                taskNote.Task = Task.Find(taskId);
                taskNote.Save();
            }
            catch (Exception err)
            {
                throw new Exception(err.StackTrace);
            }
            return true;
        }

        [AjaxPro.AjaxMethod()]
        public bool SaveTask(int taskId, int taskTypeId, string taskTitle, string taskDescription, int createMemberId, int assignedMemberId, int hoursEstimated, int hoursActual, bool isComplete, string note, int priorityId)
        {
            Task task = Task.Find(taskId) ?? new Task();
            task.TaskType = TaskType.Find(taskTypeId);
            task.Title = taskTitle;
            task.Priority = Priority.Find(priorityId);
            task.Description = taskDescription;
            task.NotificationID = 1;
            task.AssignedMemberID = assignedMemberId;
            
            if (task.ID == 0)
            {
                task.CreateMemberID = createMemberId;
                task.HoursEstimated = hoursEstimated;
                task.ClosedDateTime = Convert.ToDateTime("1/1/1900 12:00:00 AM");
                
            }
            else
            {
                task.HoursActual = hoursActual;
                if (note.Trim() != "")
                {
                    TaskNote taskNote = new TaskNote();
                    taskNote.Member = Member.Find(createMemberId);
                    taskNote.DateTime = DateTime.Now;
                    taskNote.IsSVNSource = false;
                    taskNote.Notes = note;
                    taskNote.Task = task;
                    taskNote.Save();
                }
                if (isComplete)
                {
                    task.ClosedDateTime = DateTime.Now;
                }
            }
            task.Save();
            return true;
        }

        [AjaxPro.AjaxMethod()]
        public static string GetTaskNotes(int taskId)
        {
            IList<TaskNote> notes = TaskNote.FindAll(new Order("DateTime", false), Expression.Eq("Task.ID", taskId));
            StringBuilder output = new StringBuilder();
            output.Append("<table cellpadding=3 cellspacing=0 width=100%>\n");
            using (new SessionScope())
            {
                foreach (TaskNote note in notes)
                {
                    output.Append("<tr bgcolor=\"#C7DCF5\" OnClick=\"showNote('note" + note.ID + "');\" OnMouseOver=\"this.style.backgroundColor='#9BC0ED';this.style.cursor='pointer';\" OnMouseOut=\"this.style.backgroundColor='#C7DCF5'\">\n");
                    output.Append(" <td>" + note.DateTime + "</td>\n");
                    output.Append(" <td> by " + note.Member.FirstName + " " + note.Member.LastName + "</td>\n");
                    output.Append("</tr>\n");
                    output.Append("<tr id=\"note" + note.ID + "\" style=\"display:none\">\n");
                    output.Append(" <td colspan=2>" + note.Notes + "</td>\n");
                    output.Append("</tr>\n");
                    output.Append("<tr><td colspan=2 height=1></td></tr>");
                }
            }
            if (notes.Count == 0)
            {
                output.Append("<tr>\n");
                output.Append(" <td colspan=2>No notes curently exist.</td>\n");
                output.Append("</tr>\n");
            }
            output.Append("</table>");
            return output.ToString();
        }
    }
}
